import { HealthCheckWithScreenTask, V3proLastErrCache } from "./health.check.class";
import { SendboxKey, V3proSubjectMessage } from "../model/v3pro.model";
import { HealthDescEnum, V3proAlertMessageErrTypeEnum } from "../model/v3pro.enum";
import { V3proAllStatusLambda } from "../helper/v3pro.all.status.lambda.helper";
import { V3proScreen, V3proScreenAllStatus } from "../../../api/v3pro-api/v3pro.api.response.model";
import { appConfig } from "../../../configs/env.config.load";

/**
 * 信号源输入故障
 *
 * 是否支持阈值设置： N
 *
 * 查询周期:
 * 定时查询
 * 间隔时间15秒
 * 连续3次no input则判定为故障
 *
 * 告警产生的原因:
 * 1.自动模式下，2个信号源均无输入
 * 2.当前信号源无输入
 *
 * 异常现象:
 * 发送盒带载区域画面冻住
 * 发送盒带载区域显示黑屏
 *
 * 维修建议:
 * 依次排查视频源、视频信号线
 *
 * 告警级别: 紧急
 */
const errCache = new V3proLastErrCache<SendboxKey>();
export class V3proCheckSendboxSignalInputTask extends HealthCheckWithScreenTask<SendboxKey[]> {
  checkResult = this.__checkSendBoxResult.bind(this);
  /**
   * 生成关键参数（用于 diff 计算）
   * @param allStatus
   * @param screen
   */
  async genKeyParams(allStatus: V3proScreenAllStatus, screen: V3proScreen): Promise<SendboxKey[]> {
    const [errs, normals] = V3proAllStatusLambda.filterSendBoxesWithRemove(
      allStatus,
      V3proAllStatusLambda.healthDescrWithStatusEquals(HealthDescEnum.INPUT_LOSE),
    );
    return errCache.mergeCache(
      errs.map(SendboxKey.build),
      normals.map(SendboxKey.build),
      screen.id,
      (s) => s.id,
    );
  }

  genSRMqttTopic(
    message: V3proSubjectMessage,
    screenId: string,
    allStatus: V3proScreenAllStatus,
  ): string {
    return `notify/alert/ssads/device-led-disp:signal-source-fault/${screenId}`;
    // return "信号源输入故障";
  }

  getV3proAlertMessage(): V3proAlertMessageErrTypeEnum {
    return V3proAlertMessageErrTypeEnum.SEND_BOX_SIGNAL_INPUT;
  }

  isNeedUploadFile(
    message: V3proSubjectMessage,
    screenId: string,
    allStatus: V3proScreenAllStatus,
  ): boolean {
    return message.isErrStatus();
  }

  isReportEnabled(keyParams: any, screenId: string): boolean {
    return appConfig.v3pro.errEnable.sendBoxSignal;
  }
}
